/*
 * This file (AkLoadPicture.java) is protected by the GNU GPL licence (v2). 
 * Please read the license.txt attached for further informations. 
 * 
 */

package alkwarel.shapes;

import java.io.File;

import processing.core.PImage;
import alkwarel.objects.AkArtspace;
import alkwarel.objects.AkShape;
import alkwarel.utils.AkConstants;
import artx.utils.AxLog;
import artx.utils.AxRandom;

/**
 * Loads a picture from the rsc folder
 * 
 * @author Xavier Gouchet
 * 
 */
public class AkLoadPicture extends AkShape implements AkConstants {

	/**
	 * Standard constructor.
	 */
	public AkLoadPicture() {
		super(3, "load", null);
	}

	/**
	 * @see alkwarel.objects.AkShape#Draw(alkwarel.objects.AkArtspace)
	 */
	protected boolean Draw(AkArtspace a) {
		// get list in DIR_Image
		File dir = new File(DIR_IMAGES);
		if (!dir.exists() || !dir.isDirectory()) {
			AxLog.Error("Can't access directory " + DIR_IMAGES);
			return false;
		} else {
			String[] children = dir.list();
			if (children == null) {
				AxLog.Error("Can't retrieve directory content " + DIR_IMAGES);
				return false;
			}
			if (children.length == 0) {
				AxLog.Warning("Image folder is empty");
				return false;
			}
			int fl = AxRandom.RandI(children.length);
			String path = DIR_IMAGES + children[fl];
			AxLog.Debug("AkLoadPicture::Draw =>> Loading image " + path);
			PImage p = a.loadImage(path);
			a.copy(p, 0, 0, p.width, p.height, 0, 0, a.width, a.height);
		}
		return true;
	}

}
